package com.apagg.klr;

/**
 * Android aplikasjon for å beregne KLR
 * @author Andreas Pagander
 * @version 0.1
 * @since Mars 2011
 *
 */


import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

public class klr extends Activity {
	private TextView textResultat;
	private EditText editDpl;
	private EditText editLOA;
	private EditText editLWL;
	private EditText editBredde;
	private EditText editDyp;
	private Button buttonRegnUt;
	private Spinner spinnerRiggF;
	private Spinner spinnerKjolF;
	private Spinner spinnerPropF;
	private Long sailArea;
	private EditText editKlyver;
	private EditText editFokk;
	private EditText editTop;
	private EditText editMesan;
	private EditText editStor;
	public TextView textSA;
	public TextView textMedSpi;

	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        setContentView(R.layout.main);
        // Sett til 0 for å ungå feil
        sailArea = (long) 0;
        editDpl = (EditText) findViewById(R.id.editDpl);
		editLOA = (EditText) findViewById(R.id.editLOA);
		editLWL = (EditText) findViewById(R.id.editLWL);
		editBredde = (EditText) findViewById(R.id.editBredde);
		editDyp = (EditText) findViewById(R.id.editDyp);
		editKlyver = (EditText) findViewById(R.id.editKlyver);
		editFokk = (EditText) findViewById(R.id.editFokk);
		editStor = (EditText) findViewById(R.id.editStor);
		editTop = (EditText) findViewById(R.id.editTop);
		editMesan = (EditText) findViewById(R.id.editMesan);
		spinnerRiggF = (Spinner) findViewById(R.id.spinnerRiggF);
		spinnerKjolF = (Spinner) findViewById(R.id.spinnerKjolF);
		spinnerPropF = (Spinner) findViewById(R.id.spinnerPropF);
		buttonRegnUt = (Button) findViewById(R.id.buttonRegnUt);
		buttonRegnUt.setEnabled(false);
		
        textResultat = (TextView) findViewById(R.id.textResultat);
        textMedSpi = (TextView) findViewById(R.id.textMedSpi);
        textSA = (TextView) findViewById(R.id.textSA2);
        
        MyTextWatcher myTextWatcher = new MyTextWatcher();
        MySAWatcher mySAWatcher = new MySAWatcher();
        
        editLOA.addTextChangedListener(myTextWatcher);
        editBredde.addTextChangedListener(myTextWatcher);
        editDpl.addTextChangedListener(myTextWatcher);
        editDyp.addTextChangedListener(myTextWatcher);
        editLWL.addTextChangedListener(myTextWatcher);
        editKlyver.addTextChangedListener(mySAWatcher);
        editFokk.addTextChangedListener(mySAWatcher);
        editStor.addTextChangedListener(mySAWatcher);
        editTop.addTextChangedListener(mySAWatcher);
        editMesan.addTextChangedListener(mySAWatcher);
        
        ArrayAdapter<CharSequence> adapterRiggF = ArrayAdapter.createFromResource(
	            this, R.array.lableRiggF, android.R.layout.simple_spinner_item);
	    adapterRiggF.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
	    spinnerRiggF.setAdapter(adapterRiggF);
	    
	    ArrayAdapter<CharSequence> adapterKjolF = ArrayAdapter.createFromResource(
	            this, R.array.lableKjolF, android.R.layout.simple_spinner_item);
	    adapterKjolF.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
	    spinnerKjolF.setAdapter(adapterKjolF);
	    
	    ArrayAdapter<CharSequence> adapterPropF = ArrayAdapter.createFromResource(
	            this, R.array.lablePropF, android.R.layout.simple_spinner_item);
	    adapterPropF.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
	    spinnerPropF.setAdapter(adapterPropF);
        
	    spinnerRiggF.setOnItemSelectedListener(new MyOnItemSelectedListener());
	    spinnerKjolF.setOnItemSelectedListener(new MyOnItemSelectedListener());
	    spinnerPropF.setOnItemSelectedListener(new MyOnItemSelectedListener());
        buttonRegnUt.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				calculate();
		    }
        });
    }
    
    private void calculate(){
    	SpinakkerFaktor spinakkerFaktor = new SpinakkerFaktor();
    	boolean isError = false;
    	
    	Double numDpl = Double.parseDouble(editDpl.getText().toString());
		Double numLOA = Double.parseDouble(editLOA.getText().toString());
		Double numLWL = Double.parseDouble(editLWL.getText().toString());
		Double numBredde = Double.parseDouble(editBredde.getText().toString());
		Double numDyp = Double.parseDouble(editDyp.getText().toString());
		Double numRiggF = Double.parseDouble(getResources().getStringArray(R.array.valueRiggF)[spinnerRiggF.getSelectedItemPosition()].toString());
		Double numKjolF = Double.parseDouble(getResources().getStringArray(R.array.valueKjolF)[spinnerKjolF.getSelectedItemPosition()].toString());
		Double numPropF = Double.parseDouble(getResources().getStringArray(R.array.valuePropF)[spinnerPropF.getSelectedItemPosition()].toString());
    	
    	//TODO: Bedre test av feil som gir en mer presis feilmelding 
		if (numLOA <= 0 | numDpl <= 0 | numLWL <= 0 | numBredde <= 0 | numDyp <= 0  ) {
			showErrorAlert("Du kan ikke bruke 0 eller negative tall", editLOA.getId());
			isError = true;
		}
		if (!isError){
		Long numRes = Math.round(6 * (((Math.sqrt(numLOA) / Math.sqrt(numBredde))+(Math.sqrt(sailArea)/Math.cbrt(numDpl))+
				((5*Math.sqrt(numDyp))/Math.sqrt(numLOA)))) * Math.sqrt(2.43*Math.sqrt(numLWL))*numRiggF * numKjolF * numPropF);
		
		Long numResSpi = numRes + spinakkerFaktor.getSpiFaktor(numRes);
		
		textResultat.setText("KLR: " + numRes.toString());
		textMedSpi.setText("med spinakker: " + numResSpi.toString());
		}
    }
    
    private void showErrorAlert(String errorMessage, final int fieldId) {
		new AlertDialog.Builder(this).setTitle("Feil")
				.setMessage(errorMessage).setNeutralButton("Lukk",
						new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								findViewById(fieldId).requestFocus();
							}
						}).show();
	}
    
    private class MyTextWatcher implements TextWatcher {
        public MyTextWatcher() {}
        public void afterTextChanged(Editable s) {
            //Do nothing.
        }
        public void beforeTextChanged(CharSequence s, int start, int count,int after) {
            //Do nothing.
        }
        public void onTextChanged(CharSequence s, int start, int before, int count) {
        	buttonRegnUt.setEnabled(editLOA.getText().length() > 0 && editBredde.getText().length() > 0 
    				&& editDpl.getText().length() > 0 && editDyp.getText().length() > 0 && editLWL.getText().length() > 0 && sailArea > 0);
        	textResultat.setText("");
    		textMedSpi.setText("");
    	}
    }	
    
    private class MySAWatcher implements TextWatcher {
    	public MySAWatcher(){}
    	public void afterTextChanged(Editable s) {
            //Do nothing.
        }
        public void beforeTextChanged(CharSequence s, int start, int count,int after) {
            //Do nothing.
        }
    	public void onTextChanged(CharSequence s, int start, int before, int count) {
    		Double numKlyver = editKlyver.getText().length() > 0 ? Double.parseDouble(editKlyver.getText().toString()) : 0;
    		Double numFokk = editFokk.getText().length() > 0 ? Double.parseDouble(editFokk.getText().toString()) : 0;
    		Double numStor = editStor.getText().length() > 0 ? Double.parseDouble(editStor.getText().toString()) : 0;
    		//Double numTop = editTop.getText().length() > 0 ? Double.parseDouble(editTop.getText().toString()) : 0;
    		Double numMesan = editMesan.getText().length() > 0 ? Double.parseDouble(editMesan.getText().toString()) : 0;
    		Long numSA = Math.round(numKlyver + numFokk + numStor + numMesan);
    		textSA.setText(numSA.toString());
    		sailArea = numSA;
    		buttonRegnUt.setEnabled(editLOA.getText().length() > 0 && editBredde.getText().length() > 0 
    				&& editDpl.getText().length() > 0 && editDyp.getText().length() > 0 && editLWL.getText().length() > 0 && sailArea > 0);
    		textResultat.setText("");
    		textMedSpi.setText("");
    	}
    }
    
    public class MyOnItemSelectedListener implements OnItemSelectedListener {
        public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
        	textResultat.setText("");
    		textMedSpi.setText("");
        }
        public void onNothingSelected(AdapterView<?> parent) {
          // Do nothing.
        }
    }
    
    private class SpinakkerFaktor {
    		private Long klr;
    		private Long spiFaktor;
    		
    	private Long getSpiFaktor(Long klrUtenSpi){
    		klr = klrUtenSpi;
    		if (klr <= 120){
    			spiFaktor = (long) 3;
    		}
    		else if (klr >= 151){
    			spiFaktor = (long) 5;
    		}
    		else{
    			spiFaktor = (long) 4;
    		}
    		return spiFaktor;
    	}
    }
}

